<template>
  <div class="goodsinfo-container">
    <!--小球动画-->
    <!--        半场动画只能使用钩子函数-->
    <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
      <div class="ball" v-show="ballflag" ref="ball"></div>
    </transition>
    <!--轮播图区域-->
    <div class="mui-card">
      <div class="mui-card-content">
        <swiper :lunbotuList="lunbotuList" :imgname="'src'" :isfull="isfull"></swiper>
      </div>
    </div>

    <!--购买区域-->
    <div class="mui-card">
      <div class="mui-card-header">{{goodsinfo.title}}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="price">
            市场价：
            <del>￥{{goodsinfo.market_price}}</del>&nbsp;&nbsp;销售价：
            <span class="now_price">￥{{goodsinfo.sell_price}}</span>
          </p>
          <p>
            购买数量:&nbsp;
            <!-- 这里的max是库存量 -->
            <!-- 由于goodsinfo.stock_quantity是通过ajax动态获取回来的，但是，ajax异步请求需要消耗我们的时间 -->
            <!-- 因此可能会导致这样的情况：numbox组件先于ajax渲染出来，此时，组件被渲染的时候，goodsinfo为空对象，因此传递到子组件的goodsinfo.stock_quantity是undefined -->
            <numbox :max="goodsinfo.stock_quantity" @getcount="getSelectedCount"></numbox>
          </p>
          <br />
          <p>
            <mt-button type="primary" size="small">立即购买</mt-button>
            <mt-button type="danger" size="small" @click="addToShopcar">加入购物车</mt-button>
          </p>
        </div>
      </div>
    </div>

    <!--商品详情区域-->
    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品货号：{{goodsinfo.goods_no}}</p>
          <p>库存情况：{{goodsinfo.stock_quantity}}</p>
          <p>上架时间：{{goodsinfo.add_time | dateFormat()}}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" size="large" plain @click="goodsDesc(id)">图文介绍</mt-button>
        <mt-button type="danger" size="large" plain @click="goodsComment(id)">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>
<script>
import swiper from "../subcomponent/Swiper.vue";
import numbox from "../subcomponent/goodsinfo_numbox.vue";
import {mapMutations} from "vuex"
export default {
  data() {
    return {
      id: this.$route.params.id,
      lunbotuList: [], //获取当前id轮播图的图片
      goodsinfo: {},
      isfull: false, //轮播图不设置宽度为100%
      ballflag: false,
      selectedCount: 1
    };
  },
  created() {
    this.getLunBoTu();
    this.getGoodsInfo();
  },

  methods: {
    // 方法开始
    getLunBoTu() {
      this.$http
        .get("http://api.cms.liulongbin.top/api/getthumimages/" + this.id)
        .then((res) => {
        
          if (res.data.status === 0) {
            this.lunbotuList = res.data.message;
          } else {
            Toast("加载轮播图失败");
          }
        })
        .catch(function (err) {
          console.log(err);
        });
    },

    getGoodsInfo() {
      this.$http
        .get("http://api.cms.liulongbin.top/api/goods/getinfo/" + this.id)
        .then((result) => {
          if (result.data.status === 0) {
            this.goodsinfo = result.data.message[0];
          } else {
            Toast("图片获取失败");
          }
        });
    },
    goodsDesc(id) {
      this.$router.push({ name: "goodsdesc", params: { id } });
    },
    goodsComment(id) {
      this.$router.push({ name: "goodsComment", params: { id } });
    },
    
    ...mapMutations(["addToCar",]),

    addToShopcar() {
      this.ballflag = !this.ballflag;
      //保存商品信息{id:商品的id,count:选择商品的数量,price:商品的单价,selected:商品是否为选中状态，默认为true}
      var goodsinfo = {
        id: this.id,
        count: this.selectedCount,
        price: this.goodsinfo.sell_price,
        selected: true,
      };
      //点击加入购物车时，将商品信息存到store仓库中
  
      this.addToCar(goodsinfo)
    },

    beforeEnter(el) {
      el.style.transform = "translate(0,0)"; //初始位置开始做动画
    },
    enter(el, done) {
      el.offsetWidth; //不加这个没有动画
      //获取小球的位置
      const ballPos = this.$refs.ball.getBoundingClientRect();
      //获取购物车徽标的位置
      const badgePos = document.getElementById("badge").getBoundingClientRect();
      //计算x距离
      const x = badgePos.left - ballPos.left;
      //计算y距离
      const y = badgePos.top - ballPos.top;
      el.style.transform = `translate(${x}px,${y}px)`;
      el.style.transition = "all 0.3s cubic-bezier(.15,-0.39,1,.59)";
      done(); //半场动画
    },
    afterEnter(el) {
      this.ballflag = !this.ballflag;
    },
    //获取选中小球数量
    getSelectedCount(count) {
      this.selectedCount = count;
     
    },
    // 方法结束
  },

  components: { swiper, numbox },
};
</script>
<style lang="scss" scoped>
.goodsinfo-container {
  background-color: #eee;
  overflow: hidden;
  .ball {
    width: 15px;
    height: 15px;
    background-color: red;
    border-radius: 50%;
    position: absolute;
    z-index: 9999;
    top: 390px;
    left: 142px;
  }
  .now_price {
    color: red;
    font-size: 16px;
    font-weight: bold;
  }
  .mui-card-footer {
    display: block;
    button {
      margin: 15px 0;
    }
  }
}
</style>